var ns, app;
app = new Vue({
    el: '#app',
    data: {
        IsOrderReceive: false,//接单
        OrderModel: {},
        form: {
            pageindex: 0,
            pagesize: 10,
            status: 1,//1-待接单(1,5) 2-配送中(3,6)
        },
        // 列表状态  status  1 上门取货  5 送货上门  3上门取货  6送货上门
        // 配送状态 1=待接单 2=已接单 3=已取货 4=已送达 5=洗衣完成 6=送货中 7=已完成 8=已关闭
        list: [],
        ////https://test.saasyy.com/upload/WechatServiceImg/20220211/8d307ac2fc074381b9db0f54a7ce0601_picture_f5303b3.jpg
        // list:[
        //     {"id":1,"customerName":"李文静","customerPhone":"18711111111","OrderStatus":1,"OrderStatusDesc":"派送中","status":5,"statusDesc":"送货上门","fromAddress":"社区配送站-北大门收货点","toAddress":"东岸国际-蔷薇馆-1栋1单元-109","appointTime":"今日18:00-18:30","goodWeight":"4.79","isTimingOut":'即将超时',"remark":"都是买的水果，请轻拿轻放，谢谢",orderItemList:[]},
        //     {"id":2,"customerName":"李文静1","customerPhone":"18211111111","OrderStatus":1,"OrderStatusDesc":"待取衣","status":3,"statusDesc":"上门取货","fromAddress":"社区配送站-北大门收货点","toAddress":"东岸国际-蔷薇馆-1栋1单元-109","appointTime":"今日18:00-18:30","goodWeight":"","isTimingOut":'',"remark":"",orderItemList:[]},
        //     {"id":3,"customerName":"李文静2","customerPhone":"18511111111","OrderStatus":1,"OrderStatusDesc":"派送中","status":2,"statusDesc":"快递配送","fromAddress":"社区配送站-北大门收货点","toAddress":"东岸国际-蔷薇馆-1栋1单元-109","appointTime":"今日18:00-18:30","goodWeight":"","isTimingOut":'',"remark":"都是买的水果，请轻拿轻放，谢谢",orderItemList:[]},
        // ],
        total: {
            acceptCount: 0,
            deliveryCount: 0,
        },
        current_item_length: 0, 
        can_scroll: false,
        scroll_top: 0,
        CanSendDelivery:false,//是否有派单功能权限
    },
    methods: {
        getStatusCount: function () {
            var that = this;
            var options = {};
            options.action = "getdeliverystatuscount";
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (succeed) {
                    that.total.acceptCount = data.acceptCount;
                    that.total.deliveryCount = data.deliveryCount;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        get_data: function () {
            var that = this;
            that.getStatusCount();//获取配送中心状态数量
            that.can_scroll = false;
            var options = {};
            options.pageSize = that.form.pagesize;
            options.pageIndex = that.form.pageindex;
            options.status = that.form.status;
            options.action = "getdeliverylist";
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (succeed) {
                    if (data.response.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.response;
                    } else {
                        that.list = that.list.concat(data.response);
                    }
                    that.current_item_length = that.list.length;
                    that.CanSendDelivery=data.CanSendDelivery;
                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_select_tab: function (status) {
            var that = this;
            that.current_item_length = 0;
            that.form.pageindex = 0;
            that.form.pagesize = 10;
            that.form.status = status;
            that.get_data();
        },
        do_open: function (item) {
            var that = this;
            ns.openWin('distribution_crenter_detail', '详情', {
                ID: item.id
            });
        },
        tellCallPhone: function (PnoneNumber) {
            if (!PnoneNumber) {
                api.toast({
                    msg: '联系电话为空',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.confirm({
                title: '提示',
                msg: '确定拨打电话',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        api.call({
                            type: 'tel_prompt',
                            number: PnoneNumber
                        });
                    };
                };
            });
        },
        doCloseRoom: function () {
            var that = this;
            that.IsOrderReceive = false;
        },
        //派单
        doOrderDelivry:function(item){
            var that = this;
            ns.openWin('choosedeliveryuser_frm', "选择配送员", {
                id: item.id,
            });
        },
        //接单
        doOrderReceive: function (item) {
            var that = this;
            that.OrderModel = item;
            that.IsOrderReceive = true;
        },
        doOrderReceiveSave: function () {
            var that = this;
            var options={
                action: 'acceptdelivery',
                id: that.OrderModel.id,
            };
            // console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(err));
                // console.log(JSON.stringify(data));
                if (succeed) {
                    if(data.status){
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        setTimeout(() => {
                            that.doCloseRoom();
                            api.sendEvent({
                                name: 'do_load_data',
                            });
                        }, 2000);
                    }
                    else{
                        if(data.Msg){
                            api.toast({
                                msg: data.Msg,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }   
                    }  
                    return;    
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                that.doCloseRoom();
            });
        },
        //配送完成
        doOrderService: function (item) {
            var that = this;
            ns.openWin('distribution_send_ok', '配送完成', {
                id: item.id,
            });
            // api.confirm({
            //     title: '提示',
            //     msg: '确认配送完成?',
            //     buttons: ['确认', '取消']
            // }, function (ret, err) {
            //     if (ret.buttonIndex == 1) {
            //         ns.post({
            //             action: 'confirmdelivery',
            //             id: item.id,
            //         }, function (succeed, data, err) {
            //             if (succeed) {
            //                 api.toast({
            //                     msg: '操作成功',
            //                     duration: 2000,
            //                     location: 'bottom'
            //                 });
            //                 setTimeout(() => {
            //                     api.sendEvent({
            //                         name: 'do_load_data',
            //                     });
            //                 }, 1000);
            //             } else if (err) {
            //                 api.toast({
            //                     msg: err,
            //                     duration: 2000,
            //                     location: 'bottom'
            //                 });
            //             }
            //         });
            //     }
            // });

        },
        doOrderCheck: function (item, type) {
            var title = '验收取货';
            if (type == 1) {
                title = '配送单退回';
            }
            var that = this;
            ns.openWin('distribution_exam', title, {
                ID: item.id,
                IsReject: type
            });
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.get_data();
    setTimeout(function () {
        ns.init_pull();
    }, 500)

    api.addEventListener({
        name: 'do_open_qualityhistory'
    }, function (ret, err) {
        ns.openWin('distribution_record_frm', '配送完成记录', {
            isviewhistory: true,
        })
    });

    api.addEventListener({
        name: 'reloadorderlist'
    }, function (ret, err) {
        app.do_select_tab(1);
    });
    api.addEventListener({
        name: 'do_load_data'
    }, function (ret, err) {
        app.do_select_tab(2);
    });

};
